package com.lijian.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.lijian.community.dto.NoticeDTO;
import com.lijian.community.dto.PaginationDTO;
import com.lijian.community.dto.QuestionDTO;
import com.lijian.community.enums.NoticeStatusEnum;
import com.lijian.community.enums.NoticeTypeEnum;
import com.lijian.community.exception.CustomizeErrorCode;
import com.lijian.community.exception.CustomizeException;
import com.lijian.community.mapper.NoticeMapper;
import com.lijian.community.mapper.UserMapper;
import com.lijian.community.model.Notice;
import com.lijian.community.model.Question;
import com.lijian.community.model.User;
import com.lijian.community.service.NoticeService;
import com.lijian.community.utils.PagedResult;
import com.lijian.community.utils.PagedResultUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public PaginationDTO<NoticeDTO> findAll(Integer id, Integer page, Integer size) {
        /**
         * 分页
         */
        PageHelper.startPage(page, size);
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_receiver", id);
        queryWrapper.orderByDesc("create_time");
        List<Notice> noticeList = noticeMapper.selectList(queryWrapper);
        PagedResultUtil<Notice> resultUtil = new PagedResultUtil();
        PagedResult pagedResult = resultUtil.setPagedResult(noticeList);

        PaginationDTO paginationDTO = new PaginationDTO();
        Long totalCountL = pagedResult.getRecords();
        //totalCountL转成int型
        Integer totalCount = Math.toIntExact(totalCountL);
        paginationDTO.setPagination(totalCount, page, size);

        if(noticeList.size() == 0){
            return paginationDTO;
        }
        List<NoticeDTO> noticeDTOList = new ArrayList<>();

        for (Notice notice : noticeList) {
            NoticeDTO noticeDTO = new NoticeDTO();
            BeanUtils.copyProperties(notice, noticeDTO);
            noticeDTO.setTypeName(NoticeTypeEnum.nameOfType(notice.getType()));
            noticeDTOList.add(noticeDTO);
        }
        paginationDTO.setData(noticeDTOList);
        return paginationDTO;
    }

    @Override
    public Integer unreadCount(Integer id) {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_receiver", id);
        queryWrapper.eq("status", NoticeStatusEnum.UNREAD.getStatus());
        Integer cont = noticeMapper.selectCount(queryWrapper);
        return cont;
    }

    @Override
    public NoticeDTO read(Integer id, User user) {

        Notice notice = noticeMapper.selectById(id);
        if(notice == null){
            throw new CustomizeException(CustomizeErrorCode.NOTIFICATION_NOT_FOUND);
        }

        if(!notice.getUserReceiver().equals(user.getId())){
            throw new CustomizeException(CustomizeErrorCode.READ_NOTIFICATION_FAIL);
        }

        notice.setStatus(NoticeStatusEnum.READ.getStatus());
        noticeMapper.updateById(notice);
        NoticeDTO noticeDTO = new NoticeDTO();
        BeanUtils.copyProperties(notice, noticeDTO);
        noticeDTO.setTypeName(NoticeTypeEnum.nameOfType(notice.getType()));
        return noticeDTO;
    }
}
